/*
 * GResizeHandle.java
 *
 * Created on 18 maart 2008, 14:58
 *
 * @author wijnand.schepens@hogent.be
 */
package model.graphic.objects;

import controller.commands.RedrawCommand;
import controller.commands.SetSelectedCommand;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import model.graphic.locations.LowerRightLocation;
import model.graphic.locations.RigidRelativeLocation;
import view.GUI.graphic.GraphicView;

/**
 * convenience class 
 */
public class GResizeHandle extends RightRectangleHandle {
    public GResizeHandle() {
    }

    public GResizeHandle(RectangleGraphical baseNode, int minSize) {
        super(baseNode, new LowerRightLocation(baseNode.getLowerRight(), new RigidRelativeLocation(baseNode.getUpperLeft(), minSize, minSize)), 5, 5, 5);
        this.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
    }

    @Override
    public void paint(Graphics2D g) {
        if (this.isVisible() && base.isVisible()) {
            Color oldColor = g.getColor();
            Stroke oldStroke = g.getStroke();
            Font oldFont = g.getFont();
            g.setColor(backColor);
            g.fillRect(location.getX() - 5 / 2, location.getY() - 5 / 2, getWidth(), getHeight());
            g.setColor(baseColor);
            g.drawRect(location.getX() - 5 / 2, location.getY() - 5 / 2, getWidth(), getHeight());

            if (highlighted) {
                g.setColor(highlightColor);
                g.drawRect(location.getX() - 5 / 2, location.getY() - 5 / 2, getWidth(), getHeight());
                g.drawRect(location.getX() - 5 / 2 - 3, location.getY() - 5 / 2 - 3, getWidth() + 6, getHeight() + 6);
            }

            g.setStroke(oldStroke);
            g.setColor(oldColor);
            g.setFont(oldFont);
        }
    }

    public void collapse(boolean b) {
        //do nothing
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        GraphicView gv = (GraphicView) this.getParent();
        gv.notifyObservers(new SetSelectedCommand(this.getBase(), true));
        if (((RectangleGraphical) this.getBase()).isBoxed()) {
            int x = e.getX();
            int y = e.getY();
            int xn = this.getX() + x - dragEndPoint.x;
            int yn = this.getY() + y - dragEndPoint.y;
            this.drag(xn, yn);
            this.getUpperLeft().setXY(xn, yn);
            this.setLocations(this.getUpperLeft());
            gv.notifyObservers(new RedrawCommand(this.getBase()));
        } else {
            for (Graphical g : gv.getAllNodes()) {
                RectangleGraphical rg = (RectangleGraphical) g;
                if (rg.isSelected()) {
                    GResizeHandle rh = rg.getResizer();
                    int x = e.getX();
                    int y = e.getY();
                    int xn = rh.getX() + x - dragEndPoint.x;
                    int yn = rh.getY() + y - dragEndPoint.y;
                    rh.drag(xn, yn);
                    rh.getUpperLeft().setXY(xn, yn);
                    rh.setLocations(rh.getUpperLeft());
                    gv.notifyObservers(new RedrawCommand(rh.getBase()));
                }
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        GraphicView gv = (GraphicView) this.getParent();
        if (((RectangleGraphical) this.getBase()).isBoxed()) {
                RectangleGraphical rg = (RectangleGraphical) this.getBase();
                rg.setWidthHeight(rg.getWidth(), rg.getHeight());
        } else {
            for (Graphical g : gv.getAllNodes()) {
                RectangleGraphical rg = (RectangleGraphical) g;
                if (rg.isSelected()) {
                    rg.setWidthHeight(rg.getWidth(), rg.getHeight());
                }
            }
        }
    }
}
